package com.zhangkeyou.map.mapweb.domain.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zhangkeyou.map.mapweb.exception.DefinitionException;
import com.zhangkeyou.map.mapweb.exception.ErrorEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Status<T> {
    private Integer state;
    private T data;
    private String msg;

    /**
     * 默认成功
     * @param data
     */
    public Status(T data) {
        this.state = 200;
        this.msg = "success";
        this.data = data;
    }

    //自定义异常返回的结果
    public static Status defineError(DefinitionException de){
        Status result = new Status();
        result.setState(de.getErrorCode());
        result.setMsg(de.getErrorMsg());
        result.setData(null);
        return result;
    }
    //其他异常处理方法返回的结果
    public static Status otherError(ErrorEnum errorEnum){
        Status result = new Status();
        result.setState(errorEnum.getErrorCode());
        result.setMsg(errorEnum.getErrorMsg());
        result.setData(null);
        return result;
    }
}
